using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class MarketProductInfoPanel : MonoBehaviour
{
	public UILabel title;

	public UILabel description;

	[HideInInspector]
	public Product currentProduct;

	public UIGrid upgradesGrid;

	public MarketUpgradeInfoButton upgradeButton;

	public List<MarketUpgradeInfoButton> upgradeButtonsList = new List<MarketUpgradeInfoButton>();

	public virtual void SetInfo(Product product, bool setUpgrades, bool needComparison = true)
	{
	}

	public virtual void SetTitleLocalization()
	{
		if (!(title == null))
		{
			string text = ScriptLocalization.Get("Market/" + currentProduct.id + "_title");
			title.text = ((!string.IsNullOrEmpty(text)) ? text : "Нет локализации!");
		}
	}

	public virtual void SetDescriptionLocalization()
	{
		if (!(description == null))
		{
			string text = ScriptLocalization.Get("Market/" + currentProduct.id + "_info");
			description.text = ((!string.IsNullOrEmpty(text)) ? text : "Нет локализации!");
		}
	}

	public virtual void CloseInfoPanel()
	{
		base.gameObject.SetActive(false);
	}

	public virtual void SetUpgrades()
	{
		for (int i = 0; i < upgradeButtonsList.Count; i++)
		{
			Object.Destroy(upgradeButtonsList[i].gameObject);
		}
		upgradeButtonsList.Clear();
		if (currentProduct == null || upgradesGrid == null || upgradeButton == null)
		{
			return;
		}
		Product product = currentProduct;
		upgradeButton.gameObject.SetActive(false);
		if (!(currentProduct is ProductArmor))
		{
			while (product.prevProduct != null)
			{
				product = product.prevProduct;
			}
		}
		do
		{
			InitUpgradeButton(product);
			product = product.nextProduct;
		}
		while (product != null && upgradeButtonsList.Count < 3);
		upgradesGrid.enabled = true;
	}

	public void SelectUpgradeButton()
	{
		foreach (MarketUpgradeInfoButton upgradeButtons in upgradeButtonsList)
		{
			if (upgradeButtons.currentProduct == currentProduct)
			{
				upgradeButtons.Select(true);
			}
			else
			{
				upgradeButtons.Select(false);
			}
		}
	}

	private void InitUpgradeButton(Product upgradeProduct)
	{
		MarketUpgradeInfoButton marketUpgradeInfoButton = Object.Instantiate(upgradeButton);
		marketUpgradeInfoButton.transform.SetParent(upgradesGrid.transform);
		marketUpgradeInfoButton.transform.localScale = Vector3.one;
		marketUpgradeInfoButton.gameObject.SetActive(true);
		marketUpgradeInfoButton.Init(this, upgradeProduct);
		upgradeButtonsList.Add(marketUpgradeInfoButton);
	}

	public virtual string CompareParametrs(float a, float b, string format, float eps = 0.1f)
	{
		float num = a - b;
		if (num > 0f)
		{
			return (!(num < eps)) ? ("+" + num.ToString(format)) : string.Empty;
		}
		if (num < 0f)
		{
			return (!(num > 0f - eps)) ? ("-" + Mathf.Abs(num).ToString(format)) : string.Empty;
		}
		return string.Empty;
	}
}
